<?php    
    include_once dirname(__FILE__)."/../../database/database.class.php";
    include_once dirname(__FILE__)."/../../database/card.database.class.php";
    include_once dirname(__FILE__)."/../../classes/text_to_image_converter.class.php";

    $db = new Database();
    $cdb = new Card_database();
    
    $limit['page'] = (isset($_GET['page'])) ? $_GET['page'] : 0;
    $limit['pagelimit'] = (isset($_GET['pagelimit'])) ? $_GET['pagelimit'] : 100000;

    $collection_operator = (isset($_GET['collection_operator'])) ? array('operator' => $_GET['collection_operator']) : array('operator' => "OR");
    $deck_operator = (isset($_GET['deck_operator'])) ? array('operator' => $_GET['deck_operator']) : array('operator' => "OR");

    $results = $db->findCard((isset($_GET['cardname']) ? $_GET['cardname'] : ""),
                              (isset($_GET['expansions'])) ? $_GET['expansions'] : array(),
                              (isset($_GET['collections'])) ? array_merge($_GET['collections'], $collection_operator) : array(),
                              (isset($_GET['decks'])) ? array_merge($_GET['decks'], $deck_operator) : array(),
                              (isset($_GET['deck_collection_operator'])) ? $_GET['deck_collection_operator'] : "OR",
                              (isset($_GET['colors'])) ? $_GET['colors'] : array(),
                              (isset($_GET['color_operator'])) ? $_GET['color_operator'] : "OR",
                              (isset($_GET['excluded_colors'])) ? $_GET['excluded_colors'] : array(),
                              (isset($_GET['manacostlow']) && is_numeric($_GET['manacostlow'])) ? $_GET['manacostlow'] : 0,
                              (isset($_GET['manacosthigh']) && is_numeric($_GET['manacosthigh'])) ? $_GET['manacosthigh'] : 100,
                              (isset($_GET['type'])) ? $_GET['type'] : "",
                              (isset($_GET['rarity'])) ? $_GET['rarity'] : array(),
                              (isset($_GET['cardtext'])) ? $_GET['cardtext'] : "",
                              (isset($_GET['flavortext'])) ? $_GET['flavortext'] : "",
                              (isset($_GET['power'])) ? $_GET['power'] : "",
                              (isset($_GET['toughness'])) ? $_GET['toughness'] : "",
                              (isset($_GET['loyalty'])) ? $_GET['loyalty'] : "",
                              $limit);

    if(!empty($results))
    {
      echo '<table id="search_results" style="float:left; width:40%;">';
      foreach($results as $result)
      {
        $card = $cdb->getUniqueCardByNameAndExpansion($result->cardname, $result->expansionid);
        echo '<tr>
                <td><img src="./images/logo/'.$result->expansioninternalname.'.png" alt="'.$result->expansionname.'" width="32"></td>
                <td id="' .$card->ID. '|' .$card->cardside.'" class="cardname"><a class="search_result_cardname" href="card.php?cardid='.$card->ID.'">'.$result->cardname.'</a></td>
                <td>'.text_to_image_converter::convert_manacost_to_symbol($result->Mana_Cost).'</td>
                <td>'.$result->Types.'</td>
              </tr>';
      }
      echo "</table>";
      
      echo '<div id="search_result_distro" style="float:left; border:1px solid black; position:relative; width:55%;">
              <h4>Hover over a cardname.</h4>
            </div>';
    }
    else
    {
      echo "<center><h4>No results found</h4></center>";
    }
?>